/*
 * Created by SharpDevelop.
 * User: kktn
 * Date: 11.07.2006
 * Time: 10:02
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using TagTool.Devices;

namespace TagTool.Devices.Joystick
{
	public enum JoystickMessage : uint
	{
		JoystickOneMove = 0x3A0,
		JoystickTwoMove = 0x3A1,
		JoystickOneZMove = 0x3A2,
		JoystickTwoZMove = 0x3A3,
		JoystickOneButtonDown = 0x3B5,
		JoystickTwoButtonDown = 0x3B6,
		JoystickOneButtonUp = 0x3B7,
		JoystickTwoButtonUp = 0x3B8
	}

	public enum JoystickError : uint
	{
		NoError = 0,
		BadParams = ResourceBase.JoystickError + 5,
		RequestNotCompleted = ResourceBase.JoystickError + 6,
		Unplugged = ResourceBase.JoystickError + 7
	}

    public enum JoystickButton : uint
	{
		Button_1 = 0x0001,
		Button_2 = 0x0002,
		Button_3 = 0x0004,
		Button_4 = 0x0008,
		Button_1_Change = 0x0100,
		Button_2_Change = 0x0200,
		Button_3_Change = 0x0400,
		Button_4_Change = 0x0800
	}

    public enum JoystickExtButton : long
	{
		Button_5 = 0x00000010L,
		Button_6 = 0x00000020L,
		Button_7 = 0x00000040L,
		Button_8 = 0x00000080L,
		Button_9 = 0x00000100L,
		Button_10 = 0x00000200L,
		Button_11 = 0x00000400L,
		Button_12 = 0x00000800L,
		Button_13 = 0x00001000L,
		Button_14 = 0x00002000L,
		Button_15 = 0x00004000L,
		Button_16 = 0x00008000L,
		Button_17 = 0x00010000L,
		Button_18 = 0x00020000L,
		Button_19 = 0x00040000L,
		Button_20 = 0x00080000L,
		Button_21 = 0x00100000L,
		Button_22 = 0x00200000L,
		Button_23 = 0x00400000L,
		Button_24 = 0x00800000L,
		Button_25 = 0x01000000L,
		Button_26 = 0x02000000L,
		Button_27 = 0x04000000L,
		Button_28 = 0x08000000L,
		Button_29 = 0x10000000L,
		Button_30 = 0x20000000L,
		Button_31 = 0x40000000L,
		Button_32 = 0x80000000L
	}

    public enum JoystickExtInfoFlag : long
	{
		PointOfViewCentered = -1,
		PointOfViewForward = 0,
		PointOfViewRight = 9000,
		PointOfViewBackward = 18000,
		PointOfViewLeft = 27000,
		ReturnX = 0x00000001L,
		ReturnY = 0x00000002L,
		ReturnZ = 0x00000004L,
		ReturnRudder = 0x00000008L,
		ReturnU = 0x00000010L,     //axis 5
		ReturnV = 0x00000020L,     // axis 6
		ReturnPOV = 0x00000040L,
		ReturnButtons = 0x00000080L,
		ReturnRawData = 0x00000100L,
		ReturnPOVCTS = 0x00000200L,
		ReturnCentered = 0x00000400L,
		UseDeadZone = 0x00000800L,
		ReturnAll = (ReturnX | ReturnY | ReturnZ |
		             ReturnRudder | ReturnU | ReturnV |
		             ReturnPOV | ReturnButtons),
		CalibrationReadAlways = 0x00010000L,
		CalibrationReadXY = 0x00020000L,
		CalibrationReadThree = 0x00040000L,
		CalibrationReadFour = 0x00080000L,
		CalibrationReadX = 0x00100000L,
		CalibrationReadY = 0x00200000L,
		CalibrationReadFive = 0x00400000L,
		CalibrationReadSix = 0x00800000L,
		CalibrationReadZ = 0x01000000L,
		CalibrationReadRudder = 0x02000000L,
		CalibrationReadU = 0x04000000L,
		CalibrationReadV = 0x08000000L
	}

    public enum JoystickId
	{
		One = 0,
		Two = 1
	}

    [Flags]
	public enum JoystickDriverCaps : uint
	{
		HasZ = 0x0001, 		 // Joystick has z-coordinate information
		HasRudder = 0x0002,	 // Joystick has rudder (4th axis) information
		HasU = 0x0004, 		 // Joystick has u-coordinate (5th axis) information
		HasV = 0x0008,	 	 // Joystick has v-coordinate (6th axis) information
		HasPOV = 0x0010,	 // Joystick has point of view information
		HasPOV4DIR = 0x0020, // Joystick's pov supports discrete values (centered, forward, backward, left, and right)
		HasCTS = 0x0040		 // Joystick's pov supports continuous degree bearings
	}

	/// <summary> Represents joystick capabilities </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct JoystickCaps
	{
        public uint ManufacturerId;
	    public uint ProductId;
	    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)GeneralConstants.MaximumNameLength)]
	    public string Name;
	    public uint Xmin;
	    public uint Xmax;
	    public uint Ymin;
	    public uint Ymax;
	    public uint Zmin;
	    public uint Zmax;
	    public uint NumButtons;
	    public uint PeriodMin;
	    public uint PeriodMax;
	    public uint RudderMin;
	    public uint RudderMax;
	    public uint Umin;
	    public uint Umax;
	    public uint Vmin;
	    public uint Vmax;
	    public uint Caps;	// JoystickDriverCaps
	    public uint MaxAxes;
	    public uint NumAxes;
	    public uint MaxButtons;
        //[MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)GeneralConstants.MaximumNameLength)]
        //public string RegKey;
        //[MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)GeneralConstants.MaximumOEMNameLength)]
        //public string OEMVxD;
	}
	
	/// <summary> Represents joystick info </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct JoystickInfo
	{
        public uint X;
        public uint Y;
        public uint Z;
        public JoystickButton Buttons;
	}

	/// <summary> Represents joystick extended info </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct JoystickExtInfo
	{
        public uint Size;
        public uint Flags;
        public uint X;
        public uint Y;
        public uint Z;
        public uint Rudder;
        public uint U;
        public uint V;
        public JoystickButton Buttons;
        public uint ButtonNumber;
        public uint PointOfView;
		public uint ReserverdForDriver;
		public uint ReserverdForExpansion;
	}

	public static class WinJoystick
	{
		[DllImport("winmm.dll", EntryPoint = "joyGetNumDevs", CharSet = CharSet.Ansi, ExactSpelling = false, SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        public extern static uint GetNumDevs();

        [DllImport("winmm.dll", EntryPoint = "joyGetDevCaps", CharSet = CharSet.Ansi, ExactSpelling = false, SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        public extern static MMResult GetDevCaps(uint deviceId, ref JoystickCaps caps, uint capsSize);

        [DllImport("winmm.dll", EntryPoint = "joyGetPos", CharSet = CharSet.Ansi, ExactSpelling = false, SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        public extern static MMResult GetPos(uint deviceId, ref JoystickInfo info);

        [DllImport("winmm.dll", EntryPoint = "joyGetPosEx", CharSet = CharSet.Ansi, ExactSpelling = false, SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        public extern static MMResult GetPosEx(uint deviceId, ref JoystickExtInfo info);

        [DllImport("winmm.dll", EntryPoint = "joySetCapture", CharSet = CharSet.Ansi, ExactSpelling = false, SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        public extern static MMResult SetCapture(IntPtr handle, uint deviceId, uint period, bool changed);

        [DllImport("winmm.dll", EntryPoint = "joyReleaseCapture", CharSet = CharSet.Ansi, ExactSpelling = false, SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        public extern static MMResult ReleaseCapture(uint deviceId);

    }
}
